#include "httpimage.h"
#include <QImageReader>
#include <QDebug>
#include <QMessageBox>

httpImage::httpImage()
    :QObject()
{
    mFilm = NULL;
    mUrlImage = "";
    Grille=NULL;
    LigneGrille = -1;
    ColonneGrille = -1;
    Label = NULL;
    Hauteur = Largeur = -1;
    mAbandonne = false;
    mImage = NULL;
    connect(&req, SIGNAL(finished(QNetworkReply*)), SLOT(httpRequeteFinie(QNetworkReply*)));

}

httpImage::~httpImage()
{
}

void httpImage::setUrl(QString urlString)
{
    if(urlString.isEmpty())
        QMessageBox::information(NULL,"erreur","httpImage a recu une demande de téléchargement d'une image sans url");
    mUrlImage = urlString;
}

void httpImage::setPixmap(QPixmap *pix)
{
    mImage = pix;
}

void httpImage::setFilm(film *f)
{
    mFilm = f;
}

void httpImage::recupererImage()
{
    QUrl url(mUrlImage);
    QNetworkRequest request(url);
    req.get(request);
}

void httpImage::recupererImageFilm()
{
    if(mFilm==NULL)
    {
        emit fini();
    }
    else
    {
        if(mFilm->affiche().isEmpty())
            emit fini();
        imageDansFilm(mFilm,mFilm->affiche());
    }
}

void httpImage::imageDansGrille(QTableWidget *gri, int li, int co, QString urlString)
{
    Grille = gri;
    LigneGrille = li;
    ColonneGrille = co;
    setUrl(urlString);
    this->connect(this,SIGNAL(imageReady(QPixmap)),this,SLOT(sauverImageGrille(QPixmap)));
    recupererImage();
}

void httpImage::imageDansLabel(QLabel *lab, QString urlString, int hauteur, int largeur)
{
    Label = lab;
    Hauteur = hauteur;
    Largeur = largeur;
    setUrl(urlString);
    this->connect(this,SIGNAL(imageReady(QPixmap)),this,SLOT(sauverImageLabel(QPixmap)));
    recupererImage();
}

void httpImage::imageDansPixmap(QPixmap *pix, QString urlString)
{
    setPixmap(pix);
    setUrl(urlString);
    this->connect(this,SIGNAL(imageReady(QPixmap)),this,SLOT(sauverImagePixmap(QPixmap)));
    recupererImage();
}

void httpImage::imageDansFilm(film *f, QString urlString)
{
    mFilm = f;
    if(urlString.isEmpty())
        setUrl(f->affiche());
    else
        setUrl(urlString);
    this->connect(this,SIGNAL(imageReady(QPixmap)),this,SLOT(sauverImageFilm(QPixmap)));
    recupererImage();
}

void httpImage::abandonne()
{
    mAbandonne = true;
}

void httpImage::httpRequeteFinie(QNetworkReply* rep)
{
    QList<QByteArray> dbg = QImageReader::supportedImageFormats();
    buffer = rep->readAll();
    QPixmap p;
    bool ok = p.loadFromData(buffer,"JPEG");
    //bool ok = p.loadFromData(buffer.buffer());
    if(ok)
    {
        emit imageReady(p);
    }
}

void httpImage::sauverImageGrille(const QPixmap &pix)
{ 
    this->disconnect(this,SIGNAL(imageReady(QPixmap)),this,SLOT(sauverImageGrille(QPixmap)));
    if(mAbandonne==true)
    {
        this->deleteLater();
        return;
    }
    if(Grille!= NULL && LigneGrille>=0 && ColonneGrille>=0 && LigneGrille<=Grille->rowCount() && ColonneGrille <= Grille->columnCount())
    {
        QLabel *labelij = new QLabel();
        QPixmap copy = pix.scaled(120,120,Qt::KeepAspectRatio);

        labelij->setPixmap(copy);
        Grille->setCellWidget(LigneGrille,ColonneGrille,labelij);
        Grille->resizeRowsToContents();
        Grille->resizeColumnsToContents();
    }
    emit fini();
    this->deleteLater();
}

void httpImage::sauverImageLabel(const QPixmap &pix)
{
    this->disconnect(this,SIGNAL(imageReady(QPixmap)),this,SLOT(sauverImageLabel(QPixmap)));
    if(mAbandonne==true)
    {
        this->deleteLater();
        return;
    }
     if(Label != NULL)
     {
         QPixmap copy;
         if(Hauteur != -1 && Largeur!=-1)
             copy = pix.scaled(120,120,Qt::KeepAspectRatio);
         else
             copy = pix;

         Label->setPixmap(copy);
     }
     emit fini();
     this->deleteLater();
}

void httpImage::sauverImagePixmap(const QPixmap &pix)
{
    *mImage = pix;
    emit fini();
    this->deleteLater();
}

void httpImage::sauverImageFilm(const QPixmap &pix)
{
    mFilm->setPochette(pix);
    emit fini();
    this->deleteLater();
}
